package com.lmx.tour_management_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmx.tour_management_system.mapper.AccountTouristMapper;
import com.lmx.tour_management_system.mapper.TouristMapper;
import com.lmx.tour_management_system.pojo.AccountTourist;
import com.lmx.tour_management_system.pojo.Tourist;
import jdk.nashorn.internal.runtime.regexp.RegExp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmx
 * @since 2021-01-11
 */
@Controller
public class TouristController {

    @Resource
    TouristMapper touristMapper;


    @Resource
    AccountTouristMapper accountTouristMapper;


    /**
     * 游客账户登录
     * @param username 用户名
     * @param password 密码
     * @return 返回信息
     */
    @PostMapping("/touristLogin")
    @ResponseBody
    public String touristLogin(String username, String password, HttpSession session){

        if(username.equals("")||password.equals("")){
            return "用户名或密码不能为空！";
        }


        System.out.println("username = " + username + ", password = " + password + ", session = " + session);
        AccountTourist account = accountTouristMapper.selectOne(new QueryWrapper<AccountTourist>().eq("account_username", username));

        if(account==null){
            return "用户不存在！";
        }else if(!account.getAccountPassword().equals(password)){
            return "密码错误！";
        }


        session.setAttribute("touristAccount", account);
        return "success";

    }

    /**
     * 游客账户注销
     */
    @RequestMapping("/touristLogout")
    public String touristLogout(HttpSession session){
        session.removeAttribute("touristAccount");
        return "redirect:/toIndex";
    }


    @RequestMapping("/touristAccountRegister")
    @ResponseBody
    public String touristAccountRegister(String username,String password,String checkpassword){
        if(username.equals("")||password.equals("")||checkpassword.equals("")){
            return "请输入完整的信息！";
        }
        if(!username.matches("[a-zA-Z][\\w]{8,}")){
            return "用户名应以字母开头,由八位以上的数字,字母或下划线组成！";
        }
        if(!password.equals(checkpassword)){
            return "确认密码不一致！";
        }

        try {
            AccountTourist accountTourist = new AccountTourist(null, username, password, new Date(), new Date());
            accountTouristMapper.insert(accountTourist);
        }catch (Exception e){
            e.printStackTrace();
            return "用户名已经被使用！";
        }

        return "注册成功！";

    }





    /**
     * 输入姓名电话身份证号性别 添加游客
     * @param name
     * @param phone
     * @param idcard
     * @param gender
     * @return
     */
        @RequestMapping("/admin/tourist/add")
        public String addTourist(String name, String phone, String idcard, int gender, Model model){

            Tourist tourist = new Tourist();

            tourist.setTouristName(name);
            tourist.setTouristPhone(phone);
            tourist.setTouristIdentidycard(idcard);
            tourist.setTouristSex(gender);

            int result = touristMapper.insert(tourist);

            if(result==1){
                model.addAttribute("msg","登记成功!");
            }else {
                model.addAttribute("msg","登记失败!");
            }

            return "admin/tourist_add";
        }


        @RequestMapping("/admin/tourist/toUpdate/{id}")
        public String toUpdateTourist(@PathVariable("id") int id,Model model){
            Tourist tourist = touristMapper.selectOne(new QueryWrapper<Tourist>().eq("tourist_id", id));
            model.addAttribute("tourist",tourist);
            return "admin/tourist_update";
        }


    @RequestMapping("/admin/tourist/update")
    public String updateTourist(Tourist tourist,Model model){
        System.out.println(tourist);
        int i = touristMapper.updateById(tourist);
        if(i==1){
            model.addAttribute("msg","修改成功!");
        }else{
            model.addAttribute("msg","修改失败!");
        }
        return "admin/tourist_update";
    }


    /**
     * 根据ID删除游客 删除后再查一次.
     * @param id
     * @return
     */
        @RequestMapping("/admin/tourist/delete/{id}")
        public String deleteTourist(@PathVariable("id") int id){
            int i = touristMapper.deleteById(id);
            return "redirect:/admin/toTouristList";
        }


        @RequestMapping("/admin/tourist/query")
        public String queryTourist(String serach,Model model){

            if(serach.trim().equals("")){
                model.addAttribute("msg","请输入搜索内容!");
                return "admin/tourist_query";
            }

            /**
             * 全局搜索
             */
            List<Tourist> tourists = touristMapper.selectList(new QueryWrapper<Tourist>().like("tourist_name", serach));
            tourists.addAll(touristMapper.selectList(new QueryWrapper<Tourist>().like("tourist_phone", serach)));
            tourists.addAll(touristMapper.selectList(new QueryWrapper<Tourist>().like("tourist_identidycard", serach)));

            /**
             * 每个字段查一次的方法可能会有重复数据
             * 通过Stream流去重
             */
            List<Tourist> result = new ArrayList<>();
            for (Tourist tourist : tourists) {
                boolean b = result.stream().anyMatch(a -> a.getTouristId().equals(tourist.getTouristId()));
                if (!b) {
                    result.add(tourist);
                }
            }

            if(result.isEmpty()){
                model.addAttribute("msg","未查询到此人!");
                return "admin/tourist_query";
            }

            model.addAttribute("tourists",result);
            return "admin/tourist_list";
        }





}

